package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.CornerPathEffect
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* MessageView
* */
class MessageView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mStrokeWidth = context.resources.getDimension(R.dimen.dp_3)

    private val mPaint by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeWidth = mStrokeWidth
            pathEffect = CornerPathEffect(mStrokeWidth * 2)
            strokeCap = Paint.Cap.ROUND
            style = Paint.Style.STROKE
            color = context.resources.getColor(R.color.color_DCD217)
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            translate(cx, cy)
            mPath.apply {
                reset()
                moveTo(-cx * 9 / 10, -cy * 9 / 10)
                lineTo(cx * 9 / 10, -cy * 9 / 10)
                lineTo(cx * 9 / 10, cy * 9 / 10)
                lineTo(-cx * 9 / 10, cy * 9 / 10)
                close()
            }
            drawPath(mPath, mPaint)
            mPath.apply {
                reset()
                moveTo(-cx * 7 / 10, -cy * 7 / 10)
                lineTo(0F, -cy * 2 / 10)
                lineTo(cx * 7 / 10, -cy * 7 / 10)
            }
            drawPath(mPath, mPaint)
        }
    }
}